package juc.concurrent.semaphore;

import java.util.concurrent.Semaphore;

/**
 * 并发信号量测试
 */
public class SemaphoreDemo {

    private Semaphore semaphore = new Semaphore(1);

    public void handle(){
        try {
            //信号量获取
            semaphore.acquire();
            System.out.println(Thread.currentThread().getName()+"begain time" + System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+"end time" + System.currentTimeMillis());
            //信号量释放
            System.out.println(semaphore.getQueueLength());
            System.out.println(semaphore.hasQueuedThreads());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            semaphore.release();
        }
    }
}
